﻿/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */

 var dataID = $.getUrlVar("id");
var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");
// register modal component
Vue.component('modal', {
    template: '#modal-template',
    props: {
      show: {
        type: Boolean,
        replace:true,
        required: true,
        twoWay: true    
      },
      users:[],
      userList:'',
      query:''
    },
    created:function(){
     
    },
    compiled:function(){
         var _self = this;
        _self.loadlist('');
    },
    methods:{
        checked:function(event){
            //选择
            if($(event.currentTarget).hasClass('on')){
                $(event.currentTarget).removeClass('on');
            }else{
                $(event.currentTarget).addClass('on');

            }
        },
        ok:function(){
            //确定
            var _self = this;
            var arr = [];
            $('#uluser li.on').each(function(){
                arr.push($(this).attr('dataid'));
            });
            if(arr.length > 0)
            {
                _self.userList = arr.join(',');
                _self.$parent.showModal = false; 
                _self.$parent.shareReg(_self.userList);
            }
            else{
                alert("请选择");
            }            
        },
        cancel:function(){
            var _self = this;
            _self.$parent.showModal = false; 
        },
        search:function(){
            //搜索
            var val = $("#searchword").val();
            if(val=="") return;
            var _self = this;
            _self.loadlist(val);
        },
        loadlist:function(uname){
            //列表
            var _self = this;
            var udata = {
                uname:uname
            };
            udata = Common.jsonEncode(udata);
            Common.ajaxCall({ url: 'look_getShareUsers', datatype: 'json', type: 'get', data: udata}, function (data) {
                if(data.error_code==0){
                    var obj = data.data;
                    _self.users = obj;
                }else{
                    alert(data.msg);
                }
            });
        }
    }
})



var info=new Vue({
  el:"#app",
  data:{
    info:{},
    score:0,
    addShow: false,
    showModal : false,
    users:[],
    title:'',
    inputText: '',
    followTitle:'关注',
    collectionTitle:'收藏',
    isScore:''
  },
  compiled:function(){
    var _self = this;
    
    //文档删除
    $(document).on('click','.ooo',function(){
        _self.addShow = false;
        alert({
            close:false,
            text: '是否将其删除？',
            kind: 2,
            okEvent: function(){
                _self.deleteArticle();
            }
        });
    });
    
    $(document).on('click','.scoreBox i',function(){
        if(!$(this).hasClass('on')){
            $(this).addClass('on').prevAll().addClass('on');
            $('.pop_sure').css('color','#fff').removeClass('not');
        }else{
            $(this).removeClass('on').nextAll().removeClass('on');
        }
        if($('.scoreBox i.on').length==0){
            $('.pop_sure').css('color','#a4d7fa').addClass('not');
        }
        _self.score = $(".scoreBox i[class='on']").length;
    });
    $(document).on('change.inputText','.fillInput',function(){
        _self.inputText = $(this).val();
    });
  },
  created:function(){
    var dtd = $.Deferred();
    var _self = this;

    //文档详情
    var data = {
        articleID: dataID,
    };
    data = Common.jsonEncode(data);
    Common.ajaxCall({ url: 'look_getArticleDetail', datatype: 'json', type: 'get', data: data}, function (data) {
        if(data.error_code==0){
            var obj = data.data[0];
            _self.title = obj.title;
            _self.info = obj;
            if(_self.info.isFollow==1){
                _self.followTitle = '取消关注';
            }else{
                _self.followTitle = '关注';
            }
            if(_self.info.isCollection==1){
                _self.collectionTitle = '取消收藏';
            }else{
                _self.collectionTitle = '收藏';
            }
            if(obj.hideTitle == 1){ //show
                _self.showHeader();
            } 
            if(obj.isPreview == 1){ //有权限
                //_self.showHeader();
                _self.isScore = obj.isScore;  //是否已评价限制
                var fontSize = parseInt($('html').css('fontSize'));  //获取当前屏幕的字体
                //文档资料
                var viewer = EdoViewer.createViewer('viewer', {
                 width: $(window).width(),
                 height: $(window).height(),
                 mode: obj.showType
                });
                viewer.load_from_wo(obj.content);
            }else{
                alert("您没有预览权限!");
            }
        }else{
            alert(data.msg);
        }
    });

    var fontSize = parseInt($('html').css('fontSize'));  //获取当前屏幕的字体
    //$('#artIframe').height($(document).height()-$('.header').height()-2.3*fontSize);
    $('#viewer').height($(window).height());
    //$('#viewer').css('overflow-y','scroll');
    
  },
  methods:{
    showHeader: function (e) {
        var fontSize = parseInt($('html').css('fontSize'));  //获取当前屏幕的字体
        if($('#header').css('display')==='none'){
            if($('#viewer').length>0) $('#viewer').height($(window).height()-2.3*fontSize);
            
            $('#viewer-viewer').height($('#viewer-viewer').height()-2.3*fontSize);
            $('#section').css('paddingTop',2.3*fontSize);
            $('#header').show();
            
        }else{
            if($('#viewer').length>0) $('#viewer').height($(window).height());
            if($('#viewer-viewer').length>0){
                $('#viewer-viewer').height($(window).height());
                $('#section').css('paddingTop','0');
                $('#header').hide();
            }
        }
    },
    //文档收藏
    Collection:function(){
        var _self = this;
        var data = {
            dataID: dataID,
            type : 1,
            isCollection:_self.info.isCollection==1?0:1
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'look_addCollection', datatype: 'json', type: 'get', data: data}, function (data) {
            if(data.error_code==0){
                _self.info.isCollection=_self.info.isCollection==1?0:1;
                if(_self.info.isCollection==1){
                    _self.collectionTitle = '取消收藏';
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code==1?"moneyPig.png":"lebel.png";
                    // alert({
                    //     text: '<img src="images/'+pic+'"/>',
                    //     description: obj.str,
                    //     kind: 0
                    // });
                    alert(obj.str);
                }else{
                    _self.collectionTitle = '收藏';
                    alert(data.msg);
                }
            }else{
                alert(data.msg);
            }
        });
    },
    //文档关注
    addFollow:function(){
        var _self = this;
        _self.addShow = false;
        var data = {
            dataID: dataID,
            type : 1,
            isFollow:_self.info.isFollow==1?0:1
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'look_addFollow', datatype: 'json', type: 'get', data: data}, function (data) {
            if(data.error_code==0){
                
                _self.info.isFollow=_self.info.isFollow==1?0:1;
                if(_self.info.isFollow==1){
                    _self.followTitle = '取消关注';
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code==1?"moneyPig.png":"lebel.png";
                    // alert({
                    //     text: '<img src="images/'+pic+'"/>',
                    //     description: obj.str,
                    //     kind: 0
                    // });
                   // alert(obj.str);
                  //  alert("关注成功。");
                }else{
                  //  alert(data.msg);
                    _self.followTitle = '关注';
                }
            }else{
                alert(data.msg);
            }
        });
    },
    //文档删除
    deleteArticle:function(){
        var _self = this;
        var data = {
            articleID: dataID
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'look_deleteArticle', datatype: 'json', type: 'get', data: data}, function (data) {
            alert(data.msg);
            setTimeout(function(){ _self.back();},1000);
        });
    },
    //文档评分
    addScore:function(){
        var _self = this;
        var data = {
            articleID: dataID,
            score : _self.score
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'look_addScore', datatype: 'json', type: 'get', data: data}, function (data) {
            if(data.error_code==0){
                var obj = data.data[0];
                _self.isScore = 1;
                // var code = obj.code;
                // var pic = code==1?"moneyPig.png":"lebel.png";
                // alert({
                //     text: '<img src="images/'+pic+'"/>',
                //     description: obj.str,
                //     kind: 0
                // });
             //   alert('评分成功');
                //延迟跳转
                //setTimeout(function(){ window.history.go(-1);},1000);
                //window.location.reload();
            }else{
                alert(data.msg);
            }
            _self.score = 0;
        });
    },
    grade:function(){
        var _self = this;
        _self.addShow = false;
        //文档评分
        alert({
            close: false,
            text: '评分',
            description: '<span class="scoreBox"><i ></i><i ></i><i ></i><i ></i><i ></i></span>',
            kind: 2,
            okEvent: function(){
                _self.addScore();
            }
        });
        if(!$('.scoreBox i').hasClass('on')){
            $('.pop_sure').css('color','#a4d7fa').addClass('not');
        }else{
            $('.pop_sure').css('color','#fff').removeClass('not');
        }
    },
    add:function(){
        // var stop = 1;
        if(this.addShow == false){
            this.addShow = true;
            // stop = 0;
            // document.addEventListener('touchmove', function(e){
            //     e.preventDefault();
            //     e.stopPropagation();
            // });
        }else{
            this.addShow = false;
            // stop = 1;
            // document.addEventListener('touchmove', function(e){
            //     window.event.cancelBubble = true;
            // },true);
        }
        // document.addEventListener('touchmove', function(e){
        //     if(stop == 0){
        //         e.preventDefault();
        //         e.stopPropagation();
        //     }else{
        //         window.event.cancelBubble = true;
        //     }
        //     return false;
        // });
        // function stopBubble(e){
        //     if(stop == 0){//非IE
        //         e.stopPropagation();
        //     }
        //     else{//IE
        //         window.event.cancelBubble=true;
        //     }
        // } 
    },
    shareReg:function(userList){
        var _self = this;
        alert({
            close: false,
            text: '分享',
            description: '<div class="clearfix shareTxt"><span class="j_1 photo"><img src="images/test/pic-0.png"></span><span class="text">'+_self.title+'</span></div>',
            fillInput: true,
            kind: 2,
            okEvent: function(){
                var data = {
                    dataID: dataID,
                    userList : userList,
                    comment:_self.inputText,
                    type:1
                };
                data = Common.jsonEncode(data);
                Common.ajaxCall({ url: 'look_share', datatype: 'json', type: 'get', data: data}, function (data) {
                    if(data.error_code==0){
                        var obj = data.data[0];
                        var code = obj.code;
                        var pic = code==1?"moneyPig.png":"lebel.png";
                        alert({
                            text: '<img src="images/'+pic+'"/>',
                            description: obj.str,
                            kind: 0
                        });
                    }else{
                        alert(data.msg);
                    }
                });
            }
        });
    },
    share:function(){
        var _self = this;
        _self.showModal = true;
        $('#uluser li').removeClass("on");
        $("#searchword").val('');
    },
    lookAttach:function(id){
      window.location.href = "lookAttach.html?id=" + id + "&back=" + forword;

    },
    toComment:function(id){
      window.location.href = "comments.html?id=" + id + "&back=" + forword;
    },
    back:function(){
      window.location.href = decodeURIComponent(backUrl);
    }
  },
  watch:{

  }
});



